package com.ipc.admin.controller.gwl;

import com.ipc.admin.service.gwl.service.StaffService;
import com.ipc.common.pojo.bo.gwl.PageAndSearchBo;
import com.ipc.common.pojo.bo.gwl.StaffBo;
import com.ipc.common.pojo.vo.R;
import com.ipc.common.util.gwl.NullJudgeUtil;
import com.ipc.core.dao.gwl.StaffDao;
import com.ipc.core.entity.gwl.Staff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/staff/")
@Api(tags = "员工接口")
public class StaffController {

    @Resource
    private StaffService service;

    @ApiOperation(value = "新增员工",notes = "新增员工，要求所有信息不能为空")
    @PostMapping("add")
    public R add(@ApiParam(value = "传入的信息", required = true) @RequestBody @Valid StaffBo bo, BindingResult result){
        String info = NullJudgeUtil.nullJudge(result);
        if (info != null) {
            return R.fail(info);
        }
        return service.add(bo);
    }

    @ApiOperation(value = "根据ID删除员工")
    @PostMapping("deleteById")
    public R del(@RequestBody Map<String, Integer> map) {
        return service.del(map.get("id"));
    }

    @ApiOperation(value = "根据ID数组批量删除员工")
    @PostMapping("deleteBatch")
    public R delBatch(@RequestBody List<Integer> list) {
        return service.delBatch(list);
    }

    @ApiOperation(value = "修改员工信息")
    @PostMapping("update")
    public R update(@RequestBody StaffBo bo) {
        return service.update(bo);
    }

//    @ApiOperation(value = "查询所有员工(无分页)")
//    @GetMapping("all")
//    public R all(){
//        return R.ok(service.all());
//    }

    @ApiOperation(value = "根据ID查询员工信息")
    @GetMapping("getById")
    public R getById(int id){
        return service.getById(id);
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("selectAll")
    public R page(PageAndSearchBo bo){
        return service.page(bo);
    }

    @ApiOperation(value = "导出数据到Excel")
    @GetMapping("export")
    public R export(HttpServletResponse response){
        return service.export(response);
    }

    @ApiOperation(value = "从Excel导入数据")
    @GetMapping("import")
    public R importData(MultipartFile file){
        return service.importData(file);
    }
}